package com.gopay.webtest.vo;

import java.util.List;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

/**
 * 模拟商户订单库
 * @ExPayCacheMock.java
 * @author dongdh
 * @2016年5月17日 上午9:05:55  www.gopay.com.cn Inc.All rights reserved.
 */
public class ExPayCacheMock {

    private static CacheManager cacheManager =  CacheManager.create();
    
    /**
     * 缓存十分钟，避免太大量
     */
    private static Cache cache = new Cache("expay_order_cache_mock", 2000, false, false, 1000 * 60 *10l, 0l);

    static {
        cacheManager.addCache(cache);
    }
    
    /**
     * 当确认支付 并且返回成功的时候， 向缓存中加入数据， 等待notify回调
     * @param key
     * @param value
     */
    public static void add(String key, String value) {
        Element element = new Element(key, value);
        cache.put(element);
    }
    
    public static void addObj(String key, Object value) {
        Element element = new Element(key, value);
        cache.put(element);
        
        print();
    }
    
    public static Object getObject(String key) {
        print();
        if (cache.get(key) != null) {
            return  cache.get(key).getValue();
        }
        
        return null;
    }
    
    
    /**
     * 
     * @param key
     */
    public static void remove(String key) {
        cache.remove(key);
    } 
    
    public static String  get(String key) {
        if (cache.get(key) != null) {
            return (String) cache.get(key).getValue();
        }
        return null;
    }

    public static void print() {
        /*List<String> keys = cache.getKeys();
        for (String key : keys) {
            System.out.println("key=" + key + ",value=" + cache.get(key));
        }*/
    }
    
    public static void main(String[] args) {

    }
}
